//
//  UIBarButtonItem+Extension.swift
//  WeiboProject
//
//  Created by amos on 16/1/18.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit


extension UIBarButtonItem{
    
    
    convenience init(title: String? = nil, imageName: String? = nil, target: AnyObject?, action: Selector){
        
        self.init()
        
        //1.创建一个 Button
        let button = UIButton()
        
        if let name = imageName {
            button.setImage(UIImage(named: name), forState: UIControlState.Normal)
            
            button.setImage(UIImage(named: "\(name)_highlighted"), forState: UIControlState.Highlighted)
            
        }
        
        if let _ = title{
            
            
            //设置文字
            button.setTitle(title, forState: UIControlState.Normal)

            button.titleLabel?.font = UIFont.systemFontOfSize(14)
            //设置文字颜色
            button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
             button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
            }
        
        
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        //要设置 button 的大小才行
        button.sizeToFit()
        self.customView = button
    }

}